#!/bin/sh
#
# ircd-hybrid startup script for Slackware Linux

NAME=ircd
DAEMON=/usr/bin/$NAME
PIDFILE=/var/run/$NAME/${NAME}.pid
LOGFILE=/var/log/$NAME/${NAME}.log
CONF=/etc/$NAME/${NAME}.conf
DAEMON_ARGS="-pidfile $PIDFILE -logfile $LOGFILE"

ircd_start() {
  if [! -r $CONF ]; then
    echo "$CONF does not appear to exist.  Abort."
    exit 1
  fi

  if [ -s $PIDFILE ]; then
    echo "$NAME appears to already be running?"
    exit 1
  fi

  echo "Starting IRC daemon ..."
  mkdir -p $(dirname $PIDFILE)
  chown ircd:ircd $(dirname $PIDFILE)
  chmod 0750 $(dirname $PIDFILE)
  setpriv --reset-env --reuid $NAME $DAEMON $DAEMON_ARGS
}

ircd_stop() {
  if [ ! -s $PIDFILE ]; then
    echo "$PIDFILE does not exist or is empty."
    exit 1
  fi

  echo -n "Stopping IRC daemon..."
  if [ -r $PIDFILE ]; then
    kill $(cat $PIDFILE)
    rm -f $PIDFILE
  else
    pkill ircd
  fi
  echo " done"
}

ircd_restart() {
  ircd_stop
  sleep 1
  ircd_start
}

ircd_status() {
  if [ -e $PIDFILE ]; then
    echo "ircd is running."
  else
    echo "ircd is stopped."
    exit 1
  fi
}

case "$1" in
'start')
  ircd_start
  ;;
'stop')
  ircd_stop
  ;;
'restart')
  ircd_restart
  ;;
'status')
  ircd_status
  ;;
*)
  echo "usage $0 start|stop|restart|status"
esac
